import { orderPayApi } from "@/apis/consult"
import { getMedicalOrderDetail } from "@/apis/order"
import type { MedicalOrderDetail } from "@/types/order"
import { onMounted, ref } from "vue"

// hooks函数 --> 对组件中的一些业务功能进行封装
export const useMedic = (orderId: string) => {
    // 1. 药品订单详情
    const orderDetail = ref({} as MedicalOrderDetail)
    // 查询订单详情
    const getOrderDetail = async () => {
        // 订单详情接口
        const data = await getMedicalOrderDetail(orderId)
        // 渲染订单数据
        orderDetail.value = data
        console.log(orderDetail.value);
    }
    // 查询药品订单详情
    onMounted(() => {
        getOrderDetail()
    })

    return {
        orderDetail
    }
}


export const useConfirmPayment = () => {
    const confirmPayment = async (index: number, orderId: string) => {
        if (index === undefined) return uni.showToast({ title: '请选择支付方式', icon: 'none' })
        if (index === 0) return uni.showToast({ title: '暂不支持微信支付', icon: 'none' })

        // 调用后端接口
        const { payUrl } = await orderPayApi({
            orderId: orderId,
            paymentMethod: index + '',
            payCallback: 'http://localhost:5173/#/subpkg_consult/room/index'
        })

        // #ifdef H5
        uni.showLoading({ title: '正在跳转支付...' })
        window.location.href = payUrl
        // #endif

        // #ifdef MP-WEIXIN
        // uni.requestPayment({

        // })
        // #endif
    }
    return {
        confirmPayment
    }
}